﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using WarehouseManager.Model;
using WarehouseManager.Core;
namespace WarehouseManager.DataAccess
{
    class HanghoaAccess
    {
        dbConnection db = new dbConnection();

        public DataTable Laytoanbohh()
        {
            return db.executeSelectProcedureQueryNonParameter("selectHangHoa");
        }
        //---- Tìm kiếm theo mã HH
        public DataView timkiemMahh(string mahh)
        {
            DataView dataView = Laytoanbohh().DefaultView;
            dataView.RowFilter = string.Format("maHH like '%{0}%'", mahh);
            return dataView;
        }
        //---- Tìm kiếm theo tên hh
        public DataView timkiemtenhh(string Tenhh)
        {
            DataView dataView = Laytoanbohh().DefaultView;
            dataView.RowFilter = string.Format("tenHH like '%{0}%'", Tenhh);
            return dataView;
        }
        public DataTable timhhtheoma(string mahh)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma = new SqlParameter("@maHH", SqlDbType.Char, 10);
            ma.Value = mahh;
            listParam[0] = ma;
            return db.executeSelectProcedureQuery("tim_hh", listParam);
        }
        //---- Thêm hh
        public int themhh(Hanghoa hh)
        {

            if (timhhtheoma(hh.MaHH).Rows.Count > 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[6];
                SqlParameter mahh = new SqlParameter("@maHH", SqlDbType.VarChar, 10);
                mahh.Value = hh.MaHH;
                listParam[0] = mahh;
                listParam[1] = new SqlParameter("@tenHH", hh.TenHH);
                listParam[2] = new SqlParameter("@donGia", hh.DonGia);
                listParam[3] = new SqlParameter("@donViTinh", hh.DonViTinh);
                listParam[4] = new SqlParameter("@soLuong", hh.SoLuong);
                listParam[5] = new SqlParameter("@nhaSX", hh.NhaSX);
                db.executeInsertProcedureQuery("insertHangHoa", listParam);
                return 1;
            }
        }
        //---- Sửa hh
        public int suahh(Hanghoa hh)
        {
            if (timhhtheoma(hh.MaHH).Rows.Count > 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[6];
                SqlParameter mahh = new SqlParameter("@maHH", SqlDbType.VarChar, 10);
                mahh.Value = hh.MaHH;
                listParam[0] = mahh;
                listParam[1] = new SqlParameter("@tenHH", hh.TenHH);
                listParam[2] = new SqlParameter("@donGia", hh.DonGia);
                listParam[3] = new SqlParameter("@donViTinh", hh.DonViTinh);
                listParam[4] = new SqlParameter("@soLuong", hh.SoLuong);
                listParam[5] = new SqlParameter("@nhaSX", hh.NhaSX);
                db.executeInsertProcedureQuery("updateHangHoa", listParam);
                return 1;
            }

        }

        // Xóa hh
        public int xoahh(Hanghoa hh)
        {

            if (timhhtheoma(hh.MaHH).Rows.Count > 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[6];
                SqlParameter mahh = new SqlParameter("@maHH", SqlDbType.VarChar, 10);
                mahh.Value = hh.MaHH;
                listParam[0] = mahh;
                listParam[1] = new SqlParameter("@tenHH", hh.TenHH);
                listParam[2] = new SqlParameter("@donGia", hh.DonGia);
                listParam[3] = new SqlParameter("@donViTinh", hh.DonViTinh);
                listParam[4] = new SqlParameter("@soLuong", hh.SoLuong);
                listParam[5] = new SqlParameter("@nhaSX", hh.NhaSX);
                db.executeInsertProcedureQuery("deleteHangHoa", listParam);
                return 1;
            }
        }

    }
}
